package com.canbot.u05.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.KqGroup;
import com.canbot.u05.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 考勤库选择xuAdapter
 */

public class KqGroupAdapter extends RecyclerView.Adapter<KqGroupAdapter.PhotoItem> {

        private Context context;

        private List<KqGroup> data;

        private ArrayList<KqGroup> mSelected;

        private LayoutInflater inflater;

        private PhotoItem holder;

        public KqGroupAdapter(Context context, List<KqGroup> data) {
                this.context = context;
                setData(data);
                inflater = LayoutInflater.from(context);
                mSelected = new ArrayList<KqGroup>();

        }

        private void setData(List<KqGroup> data) {
                if (data == null) {
                        data = new ArrayList<>();
                }
                this.data = data;
        }

        @NonNull
        @Override
        public PhotoItem onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View m = inflater.inflate(R.layout.layout_kq_choose_group, viewGroup, false);
                return new PhotoItem(m);
        }

        @Override
        public void onBindViewHolder(@NonNull PhotoItem holder, int position) {
                this.holder = holder;
                holder.rl_image.setTag(position);
                holder.position = position;

                final KqGroup item = data.get(holder.getLayoutPosition());

                String name = item.getGroupName();
//                int num = item.getPerson_number();

                holder.tvFaceDatabaseFolderName.setText(name);
//                holder.tvFaceDatabaseNum.setText(num + "人");


                if (item.isChecked()){
                        mSelected.add(item);
                }
//                holder.checkBox.setChecked(item.isChecked());


                holder.rl_image.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                boolean isChecked2 = !item.isChecked();
                                Logger.e("FaceSelectGroupAdapter","isChecked2="+isChecked2);
                                // 如果是选择状态
                                if (isChecked2) {
                                        // 添加到选择的列表中
                                        mSelected.add(item);
                                        holder.checkBox.setVisibility(View.VISIBLE);
                                }
                                else {
                                        // 取消选择
                                        mSelected.remove(item);
                                        holder.checkBox.setVisibility(View.INVISIBLE);
                                }
                                // 纯粹为了显示
                                holder.checkBox.setChecked(isChecked2);
                                item.setChecked(isChecked2);
                                Logger.e("FaceSelectGroupAdapter","mSelected="+mSelected);
                        }
                });

                if (item.isChecked()){
                        holder.checkBox.setVisibility(View.VISIBLE);
                }else {
                        holder.checkBox.setVisibility(View.INVISIBLE);
                }
//                holder.checkBox.setChecked(item.isChecked());
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public int getItemCount() {
                return data.size();
        }


        class PhotoItem extends RecyclerView.ViewHolder implements View.OnClickListener {

                public int position;

                RelativeLayout rl_image;
                TextView tvFaceDatabaseFolderName;

                TextView tvFaceDatabaseNum;


                private CheckBox checkBox;

                private KqGroup photo;

                public PhotoItem(View itemView) {
                        super(itemView);
                        rl_image = itemView.findViewById(R.id.rll_image) ;
                        tvFaceDatabaseFolderName = (TextView) itemView.findViewById(R.id.item_tv_face_database_name);
//                        tvFaceDatabaseNum = (TextView) itemView.findViewById(R.id.item_tv_face_database_person_num);
                        checkBox = (CheckBox) itemView.findViewById(R.id.cb_select);

                }

                @Override
                public void onClick(View v) {

                }
        }
        public ArrayList<KqGroup> getSelected() {
                return mSelected;
        }
        public void notifySetListDataChanged(List<KqGroup>list){
                this.data = list;
                notifyDataSetChanged();
        }

        public void clearSelected(){
                mSelected.clear();
        }
}
